{% extends theme("layout.html") %}
{% import "forms.html" as forms %}
{% import "utils.html" as utils %}
{% from theme(form.meta_template) import basic_meta_editor %}
{% import theme("itemviews.html") as itemviews with context %}
{% import theme(form['content_form'].template) as content_template %}
{% set extra_head = content_template.extra_head %}
{% if content_template.basic_data_editor is defined %}
    {% set data_editor = content_template.basic_data_editor %}
{% else %}
    {% set data_editor = content_template.data_editor %}
{% endif %}
{% set title = _("Modifying '%(item_name)s'", item_name=item_name) %}

{% block head %}
    {{ super() }}
    {% if extra_head %}
        {{ extra_head() }}
    {% endif %}
{% endblock %}

{% block views %}
    {{ itemviews }}
{% endblock %}

{% block content %}
    <h1>{{ title }}</h1>
    <div id="moin-modify" class="moin-form">
        <a class="btn btn-success" style="float: right" href="{{ url_for('.download_item', item_name=item_name, mimetype='application/x.moin.download') }}">
            <span class="fa fa-download"></span> {{ _('Download') }}
        </a>
        {{ gen.form.open(form, method='post', enctype='multipart/form-data') }}
            {{ forms.render_errors(form) }}
            {{ forms.render_errors(form['meta_form']['acl']) }}
            {#
               Workaround:
               For *Draw content, hide submit button and form['comment'], since *Draw
               POSTs originate from their respective applets.
            #}
            <ul class="moin-nav nav-tabs moin-shadow">
                <li class="active"><a href="#editor" data-toggle="tab">Edit Content</a></li>
                <li><a href="#meta" data-toggle="tab">Edit Meta</a></li>
                {% if user.may.admin(fqname) %}
                    <li><a href="#acl" data-toggle="tab">Edit ACL</a></li>
                {% endif %}
                <li><a href="#help" data-toggle="tab">Help</a></li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active" id="editor">
                    {{ data_editor(form['content_form'], item_name) }}
                </div>
                <div class="tab-pane active" id="meta">
                    <div class="row">
                        <div class="col-md-6">
                            {{ basic_meta_editor(form['meta_form']) }}
                        </div>
                    </div>
                </div>

                {% set field = form['meta_form']['acl'] %}
                {% if user.may.admin(fqname) %}
                    <div class="tab-pane active" id="acl">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ gen.label(field) }}
                                    {{ gen.textarea(field, rows='1', class='form-control') }}
                                </div>
                            </div>
                        </div>
                    </div>
                {% else %}
                    {{ gen.input(field, type='hidden', class='form-control') }}
                {% endif %}

                <div class="tab-pane active" id="help">
                    {{ utils.help_on_editing(help) }}
                </div>
            </div>
            {#
            {{ gen.textarea(form['meta_text'], lang='en', dir='ltr', rows=rows_meta, cols=cols) }}
            <br />
            {{ forms.render_errors(form['meta_text']) }}
            #}
            {% if not form['content_form'].is_draw %}
                <div class="form-group">
                    {{ gen.input(form['comment'], type="text", class="form-control") }}
                </div>
                <div class="form-group">
                    {{ gen.input(type='submit', id='moin-save-text-button', value=form.submit_label, class='moin-button moin-modify-submit') }}
                    {{ gen.input(type='submit', id='moin-preview-text-button', name='preview', value=form.preview_label,
                        class='moin-button moin-modify-submit', onclick="$('#moin-modify').removeClass('moin-changed-input')",
                        title=warning) }}
                    <button class="moin-button moin-cancel" type="button">{{ _("Cancel") }}</button>
                </div>
            {% endif %}

        {{ gen.form.close() }}
    </div>

    {% if preview_rendered %}
        {# user clicked Preview button, show diff and modified item as draft #}
        {{ utils.diff_table(preview_diffs) }}
        <div class="moin-watermark" id="moin-content-data" >
            {{ preview_rendered | safe }}
        </div>
    {% endif %}

{% endblock %}

{% block options_for_javascript %}
    {%- if user.scroll_page_after_edit -%}
        <br id="moin-scroll-page-after-edit" />
    {%- endif %}
    <input id="moin-item-name" value="{{ fqname }}" >
{% endblock %}
